//
//  GYMineShopAddressViewModel.m
//  GYDemo
//
//  Created by mac on 2018/6/20.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYMineShopAddressViewModel.h"
#import "Head.h"

@implementation GYMineShopAddressModel

@end

@implementation GYMineShopAddressViewModel
#pragma mark - getter and setter
-(NSMutableArray *)datas
{
    if (!_datas) {
        _datas = [NSMutableArray new];
    }
    return _datas;
}
-(GYMineShopAddressModel *)defaultAddress
{
    if (!_defaultAddress) {
        GYMineShopAddressModel *address;
        for (GYMineShopAddressModel *addr in self.datas) {
            if ([addr.defaultflag isEqualToString:@"0"]) {
                address = addr;
                break;
            }
        }
        if (!address && self.datas.count > 1) {
            address = self.datas[0];
        }
        _defaultAddress = address;
    }
    return _defaultAddress;
}
#pragma mark - method
+(instancetype)shareInterface
{
    static GYMineShopAddressViewModel *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!manager) {
            manager = [GYMineShopAddressViewModel new];
        }
    });
    return manager;
}

-(void)requestDatas
{
    @WeakSelf;
    NSString *interface = @"getWineAddressList";
    NSMutableDictionary *para = [NSMutableDictionary new];
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        if (0 == infoModel.status) {
            NSArray *data =(NSArray*)info;
            if ([data isKindOfClass:[NSArray class]]) {
                NSMutableArray *arr = [NSMutableArray new];
                for (NSDictionary *dic in data) {
                    if ([dic isKindOfClass:[NSDictionary class]]) {
                        GYMineShopAddressModel *model = [GYMineShopAddressModel yy_modelWithJSON:dic];
                        [arr addObject:model];
                        if ([model.defaultflag ?: @"1" isEqualToString:@"0"]) {
                            selfp.defaultAddress = model;
                        }
                        [selfp setDatas:arr];
                    }
                }
            }
        }else{
            NSMutableArray *arr = [NSMutableArray new];
            [selfp setDatas:arr];
        }
    }];
    
    
    
    
//    NSString *interface = @"getWineAddressList";
//    NSMutableDictionary *para = [NSMutableDictionary new];
//    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
//        if (0 == infoModel.status) {
//            NSDictionary *data = infoModel.data;
//            if ([data isKindOfClass:[NSDictionary class]]) {
//                NSArray *list = data[@"list"] ?: @[];
//                if ([list isKindOfClass:[NSArray class]]) {
//                    NSMutableArray *arr = [NSMutableArray new];
//                    for (NSDictionary *dic in list) {
//                        if ([dic isKindOfClass:[NSDictionary class]]) {
//                            GYMineShopAddressModel *model = [GYMineShopAddressModel yy_modelWithJSON:dic];
//                            [arr addObject:model];
//                            if ([model.defaultflag ?: @"1" isEqualToString:@"0"]) {
//                                selfp.defaultAddress = model;
//                            }
//                            [selfp setDatas:arr];
//                        }
//                    }
//                }
//            }
//        }else{
//            NSMutableArray *arr = [NSMutableArray new];
//            [selfp setDatas:arr];
//        }
//    }];
}

-(void)requestDeleteIndex:(NSInteger)index
{
    @WeakSelf;
    NSString *interface = @"userAddressDelete";
    GYMineShopAddressModel *product = self.datas[index];
    NSString *proid = [NSString stringWithFormat:@"%ld",product.id];
    NSMutableDictionary *para = [NSMutableDictionary new];
    [para setObject:proid forKey:@"id"];
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSMutableArray *arr = selfp.datas;
        [arr removeObject:product];
        if (product.id == selfp.defaultAddress.id) {
            selfp.defaultAddress = nil;
        }
        [selfp setDatas:arr];
    }];
}
#pragma mark - life
-(instancetype)init
{
    if (self = [super init]) {
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"userAddress"];
    }
    return self;
}
@end
